package com.example.ware.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.ware.annotation.Token;
import com.example.ware.dao.FireWorksMapper;
import com.example.ware.model.Spage.ShowPage;
import com.example.ware.model.po.Fireworks;
import com.example.ware.model.responseData.ResponseData;
import com.example.ware.model.vo.DeclareVo;
import com.example.ware.model.vo.FireWorksVo;
import com.example.ware.service.FireWorksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/fireworks")
public class FireWorksController {

    @Autowired
    FireWorksService fireWorksService;

    @Resource
    FireWorksMapper fireWorksMapper;

    //    增加烟花
    @Token
    @PostMapping("/add")
    public ResponseData addFireWorks(@RequestBody Fireworks fireworks) {
        fireworks.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        fireworks.setNumbers(0);
        fireworks.setTotal(0);
        boolean i = fireWorksService.save(fireworks);
        if (i) {
            return new ResponseData(i, true, "添加成功", 200);
        } else {
            return new ResponseData(i, false, "添加失败", 201);
        }
    }

    //    修改烟花名
    @Token
    @PutMapping("/update")
    public ResponseData updateFireWorks(@RequestBody Fireworks fireworks) {
        UpdateWrapper<Fireworks> up = new UpdateWrapper<>();
        up.eq("id", fireworks.getId()).set("name", fireworks.getName()).set("kid", fireworks.getKid()).set("img", fireworks.getImg())
                .set("price", fireworks.getPrice()).set("weight", fireworks.getWeight());
        boolean i = fireWorksService.update(up);
        if (i) {
            return new ResponseData(i, true, "修改成功", 200);
        } else {
            return new ResponseData(i, false, "修改失败", 201);
        }
    }


    //删除烟花
    @Token
    @DeleteMapping("/delete")
    public ResponseData deleteFireWorks(Fireworks fireworks) {
        Fireworks one = fireWorksService.getById(fireworks.getId());

        if (one.getNumbers() > 0) {
            return new ResponseData(null, false, "改货物已有存货记录，不可删除", 201);
        }

        boolean i = fireWorksService.removeById(fireworks);
        if (i) {
            return new ResponseData(i, true, "修改成功", 200);
        } else {
            return new ResponseData(i, false, "修改失败", 201);
        }
    }


    //    获取烟花列表
    @Token
    @GetMapping("/list")
    public ResponseData getFireWorks(String name, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "3") Integer pageSize) {

        ShowPage showPage = new ShowPage();
        Integer pageBegin = (page - 1) * pageSize;
        showPage.setPageNum(page);
        System.out.println("888888888888888888888888888888888888888888888888888888888888888");
        System.out.println(page);
        List<FireWorksVo> result = new ArrayList<>();
        int all;
        QueryWrapper<Fireworks> qw = new QueryWrapper<>();
        if (name.equals("")) {
            result = fireWorksMapper.getList(pageBegin, pageSize);
            all = fireWorksService.list().size();
        } else {
            result = fireWorksMapper.getListForName(name, pageBegin, pageSize);
            qw.eq("name", name);
            all = fireWorksService.list(qw).size();
        }


        //总数据条数
        showPage.setSize(all);
        //每页数据条数
        showPage.setPageSize(pageSize);
        //总页数
        Integer div = showPage.getSize() / pageSize;
        Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
        showPage.setPages(pages);
        showPage.setData(result);


        if (showPage.getData() != null) {
            return new ResponseData(showPage, true, "获取成功", 200);
        } else {
            return new ResponseData(showPage, false, "获取失败", 201);
        }
    }


    //    获取所有烟花数据
    @GetMapping("/all")
    public ResponseData getFireWorks() {
        List<Fireworks> list = fireWorksService.list();
        return new ResponseData(list,true,"获取成功",200);
    }


}
